<?php
namespace app\index\controller\project;

use app\index\controller\Base;

class plan extends base_project
{
    public $tableName = "ProjectPlan";
    public function index()
    {
        $id = input("project_id");
        $map=$this->search();
        $this->page($map,10,"","id desc","project");
        $code_status = model("CodeMap")->get_value("project_plan_status");
        $data["code_status"] = array_column($code_status, "m_name", "m_value");
        $this->assign($data);
        return view();
    }
    /**
     * 搜索参数
     */
    public function search()
    {
        $post = input();
        $map=[];
      
        $map["project_id"]=$this->base_project_id;
      

        return $map;
    }
    /**
     *  添加
     */
    public function add()
    {
        $model = model('ProjectPlan');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["project_id"] =$this->base_project_id;
            $data["title"] = $post["title"];
            $data["content"] = $post["content"];
            $data["complete_time"] = $post["complete_time"];
            $data["real_ctime"] = $post["real_ctime"];

            $ProjectPlanModel = model("ProjectPlan");
            $result = $ProjectPlanModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/project/plan/index", ["project_id" => $post["project_id"]]));
        }

        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectPlanModel = model('ProjectPlan');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();

            $data["title"] = $post["title"];
            $data["content"] = $post["content"];
            $data["complete_time"] = $post["complete_time"];


            $map["id"] = $post["id"];
            $result = $ProjectPlanModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/project/plan/index", ["project_id" => $post["project_id"]]));
        }

        $id = input("id");
        $data["code_status"] = model("CodeMap")->get_value("project_plan_status");
        $data["info"] = $ProjectPlanModel->get($id);
        $data["project_info"] = model("Project")->field("id,title")->where(["id" => $data["info"]["project_id"]])->find();

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $ProjectPlanModel = model('ProjectPlan');
        $result = $ProjectPlanModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }


    public  function status(){
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["status"]=$post["status"];
           $model= model("ProjectPlan");
            $result =   $model->where(["id"=>$post["id"]])->update($data);
            if ($result) {
                $this->success("操作成功");
            }
            $this->error("操作失败");

        }
        $plan_id=input("planid");
        $project_id=$this->base_project_id;
        $plan=model("ProjectPlan")->where(["id"=>$plan_id,"project_id"=>$project_id])->with("project")->find();
        $code_status = model("CodeMap")->get_value("project_plan_status");
        $data["plan"]=$plan;
        $data["code_status"]=$code_status;
        $this->assign($data);
        return view();
    }


    public  function done(){
        $id = input("id");
        if (empty($id)) {
            $this->error("操作失败");
        }
        $project_id=$this->base_project_id;
        $ProjectPlanModel = model('ProjectPlan');
        $PlanInfoModel = model('ProjectPlaninfo');
        $c=$PlanInfoModel->where(["plan_id"=>$id,"project_id"=>$project_id,"status"=>1])->count();
        if($c>0){
            $this->error("请先处理计划里程碑详情");
        }
    
        $data["status"]=5;
        $data["real_ctime"]=date("Y-m-d");
        
        $result=model("ProjectPlan")->where(["id"=>$id,"project_id"=>$project_id])->update($data);;
        if ($result) {
            $this->success("操作成功");
        }
        $this->error("操作失败");
    }

}
